package com.axjy.web;

import com.axjy.pojo.Customer;
import com.axjy.service.CustomerService;
import com.axjy.vo.Pager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by Miles on 2018/1/31.
 */
@RestController
@RequestMapping("/customer")
public class CustomerHandler {

    @Resource
    private CustomerService customerService;


    @GetMapping("/list")
    public Pager searchListOrder(Integer offset, Integer limit, Customer customer){
        System.out.println("fslfjslfjlsfsk");
        return customerService.searchList(0,5,customer);
    }

    @GetMapping("/listz")
    public String list(Model model){
        List<Customer>list=customerService.findAllCustomer();
        model.addAttribute("list",list);
        return "customer/listz";
    }


    @PostMapping("/add")
    public String add(Customer customer, HttpServletResponse resp) throws IOException {
        String msg="";
        try {
            customerService.addCustomer(customer);
            msg="添加成功！";
        }catch (Exception e){
            msg="添加失败!";
        }
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        return "redirect:/customer/add";
    }

    public void edit(Customer customer,HttpServletResponse resp) throws IOException {
        String msg="";
        try {
            customerService.updateCustomer(customer);
            msg="修改成功！";
        }catch (Exception e){
            msg="修改失败!";
        }
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.print(msg);
        out.flush();
        out.close();
    }

    public String del(@PathVariable Integer id){
        customerService.delCustomer(id);
        return "redirect:/customer/list";
    }



}
